{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ungraded Lab:  Overfitting \n",
    "\n",
    "<img align=\"left\" src=\"./images/C1_W3_Overfitting_a.png\"     style=\" width:250px; padding: 10px; \" >\n",
    "<img align=\"left\" src=\"./images/C1_W3_Overfitting_b.png\"     style=\" width:250px; padding: 10px; \" >\n",
    "<img align=\"left\" src=\"./images/C1_W3_Overfitting_c.png\"     style=\" width:250px; padding: 10px; \" >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Goals\n",
    "In this lab, you will explore:\n",
    "- the situations where overfitting can occur\n",
    "- some of the solutions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib widget\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import Output\n",
    "from plt_overfit import overfit_example, output\n",
    "plt.style.use('./deeplearning.mplstyle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overfitting\n",
    "The week's lecture described situations where overfitting can arise. Run the cell below to generate a plot that will allow you to explore overfitting. There are further instructions below the cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")\n",
    "display(output)\n",
    "ofit = overfit_example(False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the plot above you can:\n",
    "- switch between Regression and Categorization examples\n",
    "- add data\n",
    "- select the degree of the model\n",
    "- fit the model to the data  \n",
    "\n",
    "Here are some things you should try:\n",
    "- Fit the data with degree = 1; Note 'underfitting'.\n",
    "- Fit the data with degree = 6; Note 'overfitting'\n",
    "- tune degree to get the 'best fit'\n",
    "- add data:\n",
    "    - extreme examples can increase overfitting (assuming they are outliers).\n",
    "    - nominal examples can reduce overfitting\n",
    "- switch between `Regression` and `Categorical` to try both examples.\n",
    "\n",
    "To reset the plot, re-run the cell. Click slowly to allow the plot to update before receiving the next click.\n",
    "\n",
    "Notes on implementations:\n",
    "- the 'ideal' curves represent the generator model to which noise was added to achieve the data set\n",
    "- 'fit' does not use pure gradient descent to improve speed. These methods can be used on smaller data sets. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Congratulations!\n",
    "You have developed some intuition about the causes and solutions to overfitting. In the next lab, you will explore a commonly used solution, Regularization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
